package cn.aran.user.controller;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.vo.EmailRegisterVo;
import cn.aran.basic.vo.RegisterVo;
import cn.aran.user.service.IUserService;
import cn.aran.user.domain.User;
import cn.aran.user.query.UserQuery;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.save(user);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }


    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

	
    /**
    * 查询单个
    * @Parameters: [id]
    * @Return
    */
    @GetMapping("/{id}")
    public User get(@PathVariable("id")Long id)
    {
        return userService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<User> list(){

        return userService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<User> json(@RequestBody UserQuery query)
    {
        return userService.queryData(query);
    }


    /**
    * @Title: phoneRegister
    * @Description: 手机注册
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/6 18:26
    * @Parameters: [phoneRegisterVo]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody RegisterVo phoneRegisterVo){
        try {
            userService.phoneRegister(phoneRegisterVo);
            return AjaxResult.me();
        }  catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: emailRegister
    * @Description: 邮箱注册
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/9 17:56
    * @Parameters: [phoneRegisterVo]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/register/email")
    public AjaxResult emailRegister(@RequestBody EmailRegisterVo emailRegisterVo){
        try {
            userService.emailRegister(emailRegisterVo);
            return AjaxResult.me();
        }  catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
